<%@page import="com.shsh.safemanage.model.AccidentRegisterForm"%>
<%@page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.shsh.safemanage.util.RequestUtil"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.model.ProblemTransfer"%>
<%@page import="com.shsh.safemanage.model.AbstractAccident"%>
<%@page import="com.shsh.safemanage.model.ProblemTransferFeedback"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.AccidentDao"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>
<%@page import="com.shsh.safemanage.dao.UserDao"%>
<%@page import="com.shsh.safemanage.action.ActionCreator"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.action.WriteProblemAction"%>
<%
{
Long problemID = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_ID, (Long)null);
if(problemID==null){
	return;
}
AccidentDao accidentDao = DaoCreator.createAccidentDao();
ProblemDao problemDao = DaoCreator.createProblemDao();
UserDao userDao = DaoCreator.createUserDao();
TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
RoleDao roleDaoInMain = DaoCreator.createRoleDao();

WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();
UserAction userAction = ActionCreator.createUserAction();

AbstractAccident accident = accidentDao.getInfosheetByID(problemID);

int[] accidentTime = Util.getDate(accident.getAccidentTime());
int monthEnd = Util.getMonthEnd(accidentTime[0], accidentTime[1]);

User user = (User) session.getAttribute(Constants.SESSION_USER);
JobPost post1 = titlePostManageGroupDao.getJobPostByID(user.getJobPost1ID());
JobPost post2 = titlePostManageGroupDao.getJobPostByID(user.getJobPost2ID());
JobPost post3 = titlePostManageGroupDao.getJobPostByID(user.getJobPost3ID());
Role theRole = userAction.getUserRole(request);
int role = writeProblemAction.calRole(post1, post2, post3, theRole);

session.setAttribute(Constants.SESSION_ACCIDENT_CASUALTY, accident.getCasualtyList());
session.removeAttribute(Constants.SESSION_ESCALATE_USER);
session.removeAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);

List<AccidentType> accidentTypeList = accidentDao.getAccidentTypeList();
List<ProblemTransferFeedback> feedbackList = problemDao.getProblemTransferFeedbackByProblemID(problemID);
ProblemTransfer currentTransfer = problemDao.getProblemTransfer(accident.getAccidentWriteUserID(), problemID, user.getId());

String fromPage = request.getParameter(Constants.REQUEST_PARAMETER_FROM_PAGE);
%>
<div class="hider" id="hider">
	<span>&nbsp;</span>
</div>
<div class="position">
	<img src="images/position2.jpg" style="padding: 7px 9px; float: left;" />当前位置：上海高桥分公司问题管理平台 &gt;&gt; 修改事故信息单或事故快报
</div>
<div class="tt">《事故信息单》或《事故快报》</div>
<div class="content">
	<table align="center" border="1" width="99%" cellpadding="0"
		cellspacing="0">
		<tr>
		<td class="td1" colspan="2">
			<span style="margin-left: 30px; font-weight: bold">流程跟踪：</span>
			<br />
			<span style="margin-left: 50px; color: red">
			<%=Util.formatDateTime(accident.getWriteTime())%> <%=accident.getWriteUserName()%> 填报问题
			</span>
			<%
			for (ProblemTransferFeedback feedback : feedbackList) {
			%>
			<br />
			<span style="margin-left: 50px; color: red">
			<%=Util.formatDateTime(feedback.getFeedbackTime())%>
			<%
			User theFeedbackUser = userDao.getUserByID(feedback.getUserID());
			%>
			<%=theFeedbackUser.getName()%>
			<%=feedback.getProcessMethod() == Constants.PROCESS_METHOD_CLOSE? "确认同意":feedback.getProcessMethodStr()%>
			</span>
			<%
			}
			%>
		</td>
	</tr>
		<tr>
			<td class="td1" colspan="3" height="35" id="chooseDepartEquipTD">
				<span style="margin-left: 30px; font-weight: bold">作业部（业务中心）：</span> <span><%=accident.getDepartment1Name()%></span>
				<span style="margin-left: 30px; font-weight: bold">装置：</span> <span><%=accident.getEquipmentName()%></span>
				<span style="margin-left: 20px" class="span1">事故类型：</span>
				<span>
					<%@include file="/accident/accident_type_select_section.jsp" %>
				</span>
			</td>
		</tr>
		<tr>
			<td class="td1" width="60%">			
				<span style="margin-left: 30px; font-weight: ">事故名称：</span>  <br/>
				<span style="margin-left: 30px;">
						<textarea rows="3" style="width:90%; margin-bottom:5px;" id="accidentName"><%=Util.getDisplay(accident.getAccidentName(), "")%></textarea>	
				</span>	
			</td>
			<td  class="td1" width="40%" colspan="2">
				<span style="margin-left: 30px; font-weight: ">事故发生时间：</span>  
				<br/>	
				<span style="margin-left: 30px; ">						
						<select id="startYear" onchange="refreshPeriodStart(true)">
							<%
							for(int i=2001;i<=2050;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==accidentTime[0]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						年						
						<select id="startMonth" onchange="refreshPeriodStart(true)">
							<%
							for(int i=1;i<=12;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==accidentTime[1]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						月				
						<select id="startDay" onchange="refreshPeriodStart(false)">
							<%
							for(int i=1;i<=monthEnd;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==accidentTime[2]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						日		
						<select id="startHour" onchange="refreshPeriodStart(false)">
							<%
							for(int i=0;i<=23;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==accidentTime[3]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						时
						<select id="startMinute" onchange="refreshPeriodStart(false)">
							<%
							for(int i=0;i<=59;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==accidentTime[4]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						分
				</span>	
			</td>	
		</tr>
		<tr>
		<td class="td1" colspan="3">
			<span style="margin-left: 30px; font-weight: ">经济损失初步估计：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
				<textarea rows="10" style="width:90%; margin-bottom:5px;" id="loss"><%=Util.getDisplay(accident.getLoss(), "") %></textarea>	
			</span>
		</td>
		</tr>
		<tr>
		<td class="td1" colspan="3">
			<span style="margin-left: 30px; font-weight: ">事故简况：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
				<textarea rows="10" style="width:90%; margin-bottom:5px;" id="overview"><%=Util.getDisplay(accident.getOverview(), "") %></textarea>	
			</span>
		</td>
		</tr>
		<tr>
		<td class="td1" colspan="3">
			<span style="margin-left: 30px; font-weight: ">事故处置情况说明：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
				<textarea rows="10" style="width:90%; margin-bottom:5px;" id="handle"><%=Util.getDisplay(accident.getHandle(), "") %></textarea>	
			</span>
		</td>
		</tr>
		<tr>
		<td class="td1" colspan="3">
			<span style="margin-left: 30px; font-weight: ">事故原因：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
				<textarea rows="10" style="width:90%; margin-bottom:5px;" id="reason"><%=Util.getDisplay(accident.getReason(), "") %></textarea>	
			</span>
		</td>
		</tr>
		<tr>
		<td class="td1" colspan="3">
			<span style="margin-left: 30px; font-weight: ">事故教训及防范措施：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
				<textarea rows="10" style="width:90%; margin-bottom:5px;" id="moral"><%=Util.getDisplay(accident.getMoral(), "") %></textarea>	
			</span>
		</td>
		</tr>
		<tr>
			<td class="td1" width="60%">			
				<span style="margin-left: 30px; font-weight: ">人员伤亡情况：</span>  <br/>
				<span style="margin-left: 30px;">
						<textarea rows="3" style="width:90%; margin-bottom:5px;" id="casualty"><%=Util.getDisplay(accident.getCasualty(), "") %></textarea>	
				</span>	
			</td>
			<td  class="td1" width="20%">
				<span style="margin-left: 30px; font-weight: ">重伤人数：<input type="text" id="seriousInjuryCount" name="seriousInjuryCount" style="width:30px;" value="<%=Util.getDisplay(accident.getSeriousInjuryCount(), "0") %>" /></span>
					
			</td>
			<td  class="td1" width="20%">
				<span style="margin-left: 30px; font-weight: ">死亡人数：<input type="text" id="deathCount" name="deathCount" style="width:30px;" value="<%=Util.getDisplay(accident.getDeathCount(), "0") %>" /></span>				
			</td>	
		</tr>
		<tr id="accidentCasualtyTR">
			<td colspan="3">
				<div id="accidentCasualtyDiv">
				<%@include file="/accident/accident_casualty.jsp" %>
				</div>	
			</td>
		</tr>
		<%
		if (accident.getSendbackMsg() != null) {
		%>
		<tr>
		<td class="td1" colspan="3">
			<span style="margin-left: 30px; font-weight: ">退回意见：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
				<font style="color:red;"><%=Util.getDisplay(accident.getSendbackMsg(), "") %></font>	
			</span>
		</td>
		</tr>
		<%
		}
		%>	
		<tr>
			<td class="td1" colspan="3">
				<span style="margin-left: 30px; font-weight: bold">反馈信息：</span>
				<br />
				<div id="feedbackProcessMethodDiv">
				<%@include file="/accident/feedback_process_method.jsp" %>
				</div>	
				<%
				if (roleDaoInMain.hasRight(Constants.AUTH_TYPE_ACCIDENT_FEEDBACK, theRole.getId(), Constants.PROCESS_METHOD_SENDBACK))  {
				%>
				<textarea style="width: 600px; height: 100px; margin-left: 60px;color:red;" id="sendbackMsg"></textarea>
				<%
				}
				%>			
			</td>
		</tr>
		<tr id="informedPersonsTR">
			<td class="td1" colspan="3">
				<%
				if ((role & Constants.ROLE_TYPE_ZUZHANG) > 0) {
				%>
				<div id="informPersonDiv">
				<%@include file="/problem_feedback_informed_person.jsp" %>
				</div>
				<%
				}
				%>
				<%
				if ((role & (Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_ZUZHANG | Constants.ROLE_TYPE_SHENGCHANFUBUZHANG 
						 | Constants.ROLE_TYPE_ANQUANZONGJIAN | Constants.ROLE_TYPE_SHEBEIFUBUZHANG | Constants.ROLE_TYPE_BUZHANG)) > 0) {
				%>
				<div id="chooseManageGroupUserDiv">
				<%@include file="/managegroup_select/manage_check_samepage_section.jsp"%>
				</div>
				<%
				}
				%>
				<%
				if ((role & (Constants.ROLE_TYPE_KEYUAN | Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG 
						| Constants.ROLE_TYPE_FUCHUZHANG | Constants.ROLE_TYPE_CHUZHANG)) > 0) {
				%>
				<div id="chooseChushiTransferDiv">
				<%@include file="/chushi_informed_person.jsp"%>
				</div>
				<%
				}
				%>
			</td>
		</tr>
		<tr>
			<td class="td1" colspan="3">
				<span style="margin-left: 30px" class="span1">填报人及时间：</span>
				<span><%=user.getDepartment1Name()%>-<%=user.getDepartment2Name()%>-<%=user.getDepartment3Name()%></span>
				<span><%=user.getName()%></span>
				<span><%=Util.formatDateTime(Calendar.getInstance())%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" align="center" colspan="3">
				<input type="button" value="提 交" onclick="startFeedbackInfosheet()" />
				<input type="button" value="取 消" style="margin-left: 20px" onclick="cancelFeedback()" />
			</td>
		</tr>
	</table>
</div>
<div id="selectDepart1Div" style="display: block;">
<%@include file="/accident/select_depart1ID_section.jsp" %>
</div>
<script type="text/javascript">
var accidentTypes = new Array();
<%
for (AccidentType type : accidentTypeList) {
%>
accidentTypes.push("<%=type.getId()%>_<%=type.getDepartment1ID()%>");
<%
}
%>
function startFeedbackInfosheet(){
	var casualtyName = getElementValueByID("casualtyName");
	
	if(casualtyName == null || casualtyName == '' || casualtyName == 'undefined') {
		feedbackInfosheet();
	} else {
		addAccidentCasualty(); 
		feedbackInfosheet();
	}
}

function feedbackInfosheet(){
	var processMethod = 0;
	var processRadio0 = document.getElementById('processRadio0');
	var processRadio1 = document.getElementById('processRadio1');
	var processRadio2 = document.getElementById('processRadio2');
	var processRadio3 = document.getElementById('processRadio3');
	var processRadio4 = document.getElementById('processRadio4');
	var processRadio5 = document.getElementById('processRadio5');
	var processRadio6 = document.getElementById('processRadio6');
	if (processRadio1 != null && processRadio1.checked) {
		processMethod = 1;
	} else if (processRadio2 != null && processRadio2.checked) {
		processMethod = 2;
	} else if (processRadio3 != null && processRadio3.checked) {
		processMethod = 3;
	} else if (processRadio4 != null && processRadio4.checked) {
		processMethod = 4;
	} else if (processRadio5 != null && processRadio5.checked) {
		processMethod = 5;
	} else if (processRadio6 != null && processRadio6.checked) {
		processMethod = 6;
	} else if (processRadio0 != null && processRadio0.checked) {
		processMethod = 0;
	}
	var userIDs = '';
	var userIDElements = document.getElementsByName('informedUserID');
	for (var i = 0; i != userIDElements.length; i++) {
		var userIDEle = userIDElements[i];
		if (userIDEle.checked) {
			if (userIDs == '') {
				userIDs = userIDs + userIDEle.value;
			} else {
				userIDs = userIDs + ',' + userIDEle.value;
			}
		}
	}
	
	var manageGroupUserCheckEles = document.getElementsByName('manageGroupUserCheck');
	for (var i = 0; i != manageGroupUserCheckEles.length; i++) {
		if (manageGroupUserCheckEles[i].checked) {
			if (userIDs == '') {
				userIDs = userIDs + manageGroupUserCheckEles[i].value;
			} else {
				userIDs = userIDs + ',' + manageGroupUserCheckEles[i].value;
			}
		}
	}
	
	var accidentTypeID = getElementValueByID("accidentType");
	var accidentName = getElementValueByID("accidentName");
	var casualty = getElementValueByID("casualty");
	var loss = getElementValueByID("loss");
	var overview = getElementValueByID("overview");
	var handle = getElementValueByID("handle");
	var reason = getElementValueByID("reason");
	var moral = getElementValueByID("moral");
	var seriousInjuryCount = getElementValueByID("seriousInjuryCount");
	var deathCount = getElementValueByID("deathCount");
	var sendbackMsg = getElementValueByID("sendbackMsg");
	
	var startYear = $("#startYear").val();
	var startMonth = $("#startMonth").val();
	var startDay = $("#startDay").val();
	var startHour = $("#startHour").val();
	var startMinute = $("#startMinute").val();
	
	var userIDs = '';
	var userIDElements = document.getElementsByName('informedUserID');
	for (var i = 0; i != userIDElements.length; i++) {
		var userIDEle = userIDElements[i];
		if (userIDEle.checked) {
			if (userIDs == '') {
				userIDs = userIDs + userIDEle.value;
			} else {
				userIDs = userIDs + ',' + userIDEle.value;
			}
		}
	}
	
	if(accidentTypeID == null || accidentTypeID == '' || accidentTypeID == 'undefined') {
		$.dialog.alert('请选择事故类型');
		return;
	}

	if(overview == null || overview == '' || overview == 'undefined') {
		$.dialog.alert('请填写事故简况');
		return;
	}

	if(loss == null || loss == '' || loss == 'undefined') {
		$.dialog.alert('请填写经济损失初步估计');
		return;
	}
	
	if(handle == null || handle == '' || handle == 'undefined') {
		$.dialog.alert('请填写事故处置情况说明');
		return;
	}
	if(reason == null || reason == '' || reason == 'undefined') {
		$.dialog.alert('请填写事故原因');
		return;
	}	
	
	if(moral == null || moral == '' || moral == 'undefined') {
		$.dialog.alert('请填写事故教训及防范措施');
		return;
	}
	var strExp = <%=Constants.REGX_COUNT%>;
	if(seriousInjuryCount == null ||  seriousInjuryCount == '' || seriousInjuryCount == 'undefined') {
		// empty.
	} else {
		if (!strExp.test(seriousInjuryCount)){
			$.dialog.alert('重伤人数必须为非负整数');
			return;
		}		
	}
	
	if(deathCount == null ||  deathCount == '' || deathCount == 'undefined') {
		// empty.
	} else {
		if (!strExp.test(seriousInjuryCount)){
			$.dialog.alert('死亡人数必须为非负整数');
			return;
		}		
	}
	
	if(processMethod == 4 && (sendbackMsg == null || sendbackMsg == '' || sendbackMsg == 'undefined')) {
		$.dialog.alert('请填写反馈信息');
		return;
	}
	
	var msg = "您确定反馈此报告？";
	if(processMethod == 2){
		msg = "您确定反馈此报告？<br/>注：该系统不支持组员（科员）对该报告进行操作";
	}
	
	loadPageSection(
			'<%=request.getContextPath()%>/tendency/select_depart1ID_section.jsp', 
			'selectDepart1Div', 
			{}, 
			false,
			function(){
				var warnNotNeeded = false;
				var depart1ID = getElementValueByID("selectedDepart1ID");
				if( depart1ID == null || depart1ID == '' || depart1ID == 'undefined' || depart1ID == 0){
					warnNotNeeded = true;
				}
				
				if(!warnNotNeeded){
					for(var i =0 ;i <accidentTypes.length; i++){
						var str = accidentTypes[i];
						var strs = str.split("_");
						if(Number(strs[0])==accidentTypeID && Number(strs[1]) == depart1ID){
							warnNotNeeded = true;
							break;
						}
					}
					
				}
				
				if(!warnNotNeeded) {
					msg = '事故类型与上报职能处室不符，点击确定继续，取消则更正！';
				}
				
				$.dialog.confirm(msg, function() {
					openWaitingDialog('waitingDialog');
					$.ajax({
						type:'post',
						url:'<%=request.getContextPath()%>/accident/feedback_infosheet_process.jsp',
						data:{
							id: <%=problemID%>,
							processMethod: processMethod,
							transferID: <%=(currentTransfer==null?"''":currentTransfer.getId())%>,
							feedback: '',

							accidentTypeID: accidentTypeID,
							accidentName: accidentName,
							startYear: startYear,
							startMonth: startMonth,
							startDay: startDay,
							startHour: startHour,
							startMinute: startMinute,
							casualty: casualty,
							loss: loss,
							overview: overview,
							handle: handle,
							reason: reason,
							moral: moral,
							seriousInjuryCount: seriousInjuryCount,
							deathCount: deathCount,
							sendbackMsg: sendbackMsg,

							problemTitle: accidentName,
							problemContent: '',
							zhenggai_cuoshi: '',
							userID: userIDs
						},
						success:function(data,state,xmlhttp){
							var obj = null;
							try {
								obj = eval('(' + data + ')');
							} catch (e) {
							}
							if (typeof(obj.isSuccess) != 'undefined') {
								closeWaitingDialog('waitingDialog');
								if (obj.isSuccess == 'true') {
									$.dialog('报告反馈成功！', function() { 
										loadPageSection(
												'<%=request.getContextPath()%>/<%=fromPage%>', 
												'mainContentDiv', 
												{
													id: <%=problemID%>
												}, 
												true
											); 
										});
								} else {
									$.dialog.alert(obj.error);
								}
							}
						}
					});
				});
			}
	);
}
function cancelFeedback(){
	loadPageSection(
			'<%=request.getContextPath()%>/<%=fromPage%>', 
			'mainContentDiv', 
			{
				id: <%=problemID%>
			}, 
			true
		);	
}

function refreshPeriodStart(updateMonthEnd){
	var startYear = $("#startYear").val();
	var startMonth = $("#startMonth").val();
	
	if(updateMonthEnd){
		refreshMonthEnd(startYear, startMonth, $("#startDay"));
	}
}

function chooseProcessMethod(method) {
	var sendbackMsgEle = document.getElementById('sendbackMsg');
	if(sendbackMsgEle != null ){
		sendbackMsgEle.style.display = 'none';	
		if(method == 4 ){
			sendbackMsgEle.style.display = '';		
		}	
	}
	if (method == 1) {
		document.getElementById('informedPersonsTR').style.display = 'none';
	} else if (method == 2) {
		document.getElementById('informedPersonsTR').style.display = '';
	} else if (method == 4 || method == 3 || method == 5 || method == 6 || method == 0) {
		document.getElementById('informedPersonsTR').style.display = 'none';
	}
}

chooseProcessMethod(0);
refreshPeriodStart(false);
setElementValueByID("accidentType", <%=accident.getAccidentTypeID()%>);
</script>
<%
}
%>